﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace LeyCDTMapsInGE
{
  public partial class ProgressForm : Form
  {
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern uint SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

    private bool detailsShown;
    private bool conversionComplete;
    private bool closing;
    private MainForm mainForm;

    delegate void AppendDetailCallback(string text);
    delegate void NumMapsCallback(int numMaps);
    delegate void CurrentMapCallback(string name, int index);
    delegate void ConversionDoneCallback();

    public ProgressForm(MainForm mainForm_)
    {
      InitializeComponent();
      hideDetails();
      conversionComplete = false;
      closing = false;
      mainForm = mainForm_;
      btnCancelClose.Text = "Cancel";
    }

    public void appendDetail(string text)
    {
      if (txtDetails.InvokeRequired)
      {
        AppendDetailCallback d = new AppendDetailCallback(appendDetail);
        Invoke(d, new object[] { text });
      }
      else
      {
        txtDetails.AppendText(text);
        txtDetails.Refresh();
      }
    }

    public void numMaps(int count)
    {
      if (progressBar1.InvokeRequired)
      {
        NumMapsCallback d = new NumMapsCallback(numMaps);
        Invoke(d, new object[] { count });
      }
      else
      {
        progressBar1.Maximum = count;
      }
    }

    public void currentMap(string name, int index)
    {
      if (lblMapName.InvokeRequired && progressBar1.InvokeRequired)
      {
        CurrentMapCallback d = new CurrentMapCallback(currentMap);
        Invoke(d, new object[] { name, index });
      }
      else
      {
        lblMapName.Text = name;
        progressBar1.Value = index - 1;
        lblMapName.Refresh();
        progressBar1.Refresh();
      }
    }

    public void conversionCancled()
    {
      if (lblMapName.InvokeRequired && btnCancelClose.InvokeRequired)
      {
        ConversionDoneCallback d = new ConversionDoneCallback(conversionCancled);
        Invoke(d);
      }
      else
      {
        SendMessage(progressBar1.Handle,
          0x400 + 16, //WM_USER + PBM_SETSTATE
          0x0002, //PBST_PAUSED
          0);
        lblMapName.Text = "Aborted!";
        btnCancelClose.Text = "Close";
        conversionComplete = true;
      }
    }

    public void conversionDone()
    {
      if (lblMapName.InvokeRequired && btnCancelClose.InvokeRequired)
      {
        ConversionDoneCallback d = new ConversionDoneCallback(conversionDone);
        Invoke(d);
      }
      else
      {
        progressBar1.Value = progressBar1.Maximum;
        lblMapName.Text = "Finished!";
        btnCancelClose.Text = "Close";
        conversionComplete = true;
      }
    }

    private void btnDetails_Click(object sender, EventArgs e)
    {
      toggleDetails();
    }

    private void toggleDetails()
    {
      detailsShown = !detailsShown;
      if (detailsShown)
      {
        showDetails();
      }
      else
      {
        hideDetails();
      }
    }

    private void hideDetails()
    {
      detailsShown = false;
      this.Height = 138;
      btnDetails.Text = "Details >>";
    }

    private void showDetails()
    {
      detailsShown = true;
      this.Height = 323;
      btnDetails.Text = "Hide <<";
    }

    private void btnCancelClose_Click(object sender, EventArgs e)
    {
      cancelClose();
    }

    private void ProgressForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      cancelClose();
    }

    private void cancelClose()
    {
      if (closing)
      {
        // no-op
      }
      else if (conversionComplete)
      {
        closing = true;
        Close();
      }
      else
      {
        btnCancelClose.Text = "Canceling...";
        mainForm.abort();
      }
    }
  }
}
